//
//  DiscoverController.m
//  NewProject
//
//  Created by l wb on 2018/6/1.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "DiscoverController.h"
#import "DiscoverCell.h"
#import "DiscoverDetailController.h"
@interface DiscoverController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *talbeView;
@property (weak, nonatomic) IBOutlet UIButton *firstBtn;
@property (weak, nonatomic) UIButton *selectBtn;
@property (strong, nonatomic) NSArray *newsArr;
@property (strong, nonatomic) NSArray *historyArr;
@property (strong, nonatomic) NSArray *welfareArr;
@end

@implementation DiscoverController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"发现";
    self.selectBtn = self.firstBtn;
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self titleBtnClickAction:self.selectBtn];
}
-(void)getNewsInformationDataFromNetWithType:(NSString *)type{
    BmobQuery *query = [BmobQuery queryWithClassName:@"Discover"];
    [query whereKey:@"type" equalTo:type];
   
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (!error) {
            
            if ([type isEqualToString:@"1"]) {
                self.newsArr = array;
            }else if([type isEqualToString:@"2"]){
                self.historyArr = array;
            }
            else {
                self.welfareArr = array;
            }

            [self.talbeView reloadData];
        }
       
        
    }];
    
    
}

- (IBAction)titleBtnClickAction:(UIButton *)sender {
    self.selectBtn.selected = NO;
    sender.selected = YES;
    self.selectBtn = sender;
    switch (sender.tag) {
        case 1:
            [self getNewsInformationDataFromNetWithType:@"1"];
            [self getNewsInformationDataFromNetWithType:@"1"];
            break;
        case 2:
            [self getNewsInformationDataFromNetWithType:@"2"];
            [self getNewsInformationDataFromNetWithType:@"2"];
            break;
        case 3:
            [self getNewsInformationDataFromNetWithType:@"3"];
            [self getNewsInformationDataFromNetWithType:@"3"];
            break;
        default:
            break;
    }
    [self.talbeView reloadData];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.selectBtn) {
        NSInteger count;
        switch (self.selectBtn.tag) {
            case 1:
                count = self.newsArr.count;
                break;
            case 2:
                count = self.historyArr.count;

                break;
            case 3:
                count = self.welfareArr.count;
                break;
            default:
                count = 0;
                break;
        }
        return count;
    }else{
        return 0;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DiscoverCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverCell"];
    BmobObject *object;
    
    
    switch (self.selectBtn.tag) {
        case 1:
            object = self.newsArr[indexPath.row];
            break;
        case 2:
             object = self.historyArr[indexPath.row];
            break;
        case 3:
             object = self.welfareArr[indexPath.row];
            break;
        
    }
    
    
    
    
    
    [cell.showImg sd_setImageWithURL:[object objectForKey:@"showImg"]];
    cell.titleLab.text = [object objectForKey:@"title"];
    cell.contentLab.text = [object objectForKey:@"contentOne"];
    
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 113;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DiscoverDetailController *detailVC = [[UIStoryboard storyboardWithName:@"DiscoverStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"DiscoverDetailController"];
    switch (self.selectBtn.tag) {
        case 1:
            detailVC.model = self.newsArr[indexPath.row];
            break;
        case 2:
            detailVC.model = self.historyArr[indexPath.row];
            break;
        case 3:
            detailVC.model = self.welfareArr[indexPath.row];
            break;
            
    }
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

@end
